package com.one.blocks.rbac.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

import static com.one.blocks.rbac.constant.RbacConstant.REQUEST_ERROR_KEY;


/**
 * @author linfuxin Created on 2022-06-10 09:09:03
 */
public class BizExceptionHandlerFilter implements Filter {

    private final String errorPath;

    public BizExceptionHandlerFilter(String errorPath) {
        this.errorPath = errorPath;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (RuntimeException e) {
            request.setAttribute(REQUEST_ERROR_KEY, e);
            request.getRequestDispatcher(errorPath).forward(request, response);
        }
    }
}
